package com.shyroke.calculation;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shyroke.util.CalculationUtil;



/**
 * 专家权重计算类
 * @author shyroke
 *
 */
public class ExpertWeightCal {
	private double T;
	private double I;
	private double F;

	/**
	 * 获取每个专家的权重值
	 * 其输出的权重值对应的是List<expert>中对应顺序的专家。
	 * @param experts
	 * @return
	 */
	public static List<Double> calExportWeight(List<ExpertWeightCal> experts) {

		List<Double> resultList=new ArrayList<Double>();

		/**
		 * 每个专家加的模糊行测度
		 */
		List<Double> value = new ArrayList<Double>();

		for (int i = 0; i < experts.size(); i++) {

			double u = (1 - Math.sqrt(((1 - experts.get(i).T) * (1 - experts.get(i).T)
					+ (experts.get(i).I) * (experts.get(i).I) + (experts.get(i).F) * (experts.get(i).F)) / 3));

			value.add(u);
		}

		/**
		 * 所有专家的模糊行测度之和
		 */
		double allWight = 0;

		for (int i = 0; i < value.size(); i++) {
			allWight = allWight + value.get(i);
		}

		for (int i = 0; i < value.size(); i++) {
			double weight =Double.valueOf(new DecimalFormat("#.000").format(value.get(i) / (double) allWight));

			resultList.add(weight);
		}

		return resultList;

	}
	
	public static void main(String[] args) {
		ExpertWeightCal e1=new ExpertWeightCal();
		e1.T=0.9;
		e1.I=0.1;
		e1.F=0.5;
		ExpertWeightCal e2=new ExpertWeightCal();
		e2.T=0.9;
		e2.I=0.1;
		e2.F=0.5;
		ExpertWeightCal e3=new ExpertWeightCal();
		e3.T=0.5;
		e3.I=0.5;
		e3.F=0.45;
		ExpertWeightCal e4=new ExpertWeightCal();
		e4.T=0.5;
		e4.I=0.5;
		e4.F=0.45;
		
		List<ExpertWeightCal> exList=new ArrayList<ExpertWeightCal>();
		exList.add(e1);
		exList.add(e2);
		exList.add(e3);
		exList.add(e4);
		List<Double> result=new ExpertWeightCal().calExportWeight(exList);
		System.out.println(result);
	}
	
	/**
	 * 
	 * @return key代表专家id，value代表重要性
	 * @throws IOException 
	 */
	public static Map<String,Double> getExpertWeight() throws IOException{
		Map<String,Double> map = new HashMap<>();
		List<ExpertWeightCal> experts  = new ArrayList<>();
		ExpertWeightCal ex1 = new ExpertWeightCal();
		CalculationUtil calculation = new CalculationUtil();
		Map<String,List<Double>> importanceMap =  calculation.getExpertImportance();
		List<Double> importList1 = importanceMap.get("1");
		ex1.T = importList1.get(0);
		ex1.I =  importList1.get(1);
		ex1.F =  importList1.get(2);
		
		ExpertWeightCal ex2 = new ExpertWeightCal();
		List<Double> importList2 = importanceMap.get("2");
		ex2.T =importList2.get(0);
		ex2.I = importList2.get(1);
		ex2.F = importList2.get(2);
		
		ExpertWeightCal ex3 = new ExpertWeightCal();
		List<Double> importList3 = importanceMap.get("3");
		ex3.T = importList3.get(0);
		ex3.I =  importList3.get(1);
		ex3.F = importList3.get(2);
		
		ExpertWeightCal ex4 = new ExpertWeightCal();
		List<Double> importList4 = importanceMap.get("4");
		ex4.T = importList4.get(0);
		ex4.I = importList4.get(1);
		ex4.F = importList4.get(2);
		experts.add(ex1);
		experts.add(ex2);
		experts.add(ex3);
		experts.add(ex4);
		
		List<Double> doubleList=ExpertWeightCal.calExportWeight(experts);
		for(int i=1;i<=doubleList.size();i++) {
			map.put(i+"", doubleList.get(i-1));
		}
		System.out.println("========================="+doubleList);
		return map;
	}
	
}
